package com.ctshk.rpc.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.InformType;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.IsRead;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.user.dto.UserMessageDTO;
import com.ctshk.rpc.user.dto.UserMessageListDTO;
import com.ctshk.rpc.user.entity.UserCollection;
import com.ctshk.rpc.user.entity.UserMessage;
import com.ctshk.rpc.user.mapper.UserMessageMapper;
import com.ctshk.rpc.user.req.UserMessagePageReq;
import com.ctshk.rpc.user.req.UserMessageReq;
import com.ctshk.rpc.user.service.IUserMessageService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * <p>
 *
 * </p>
 *
 * @author 李新科
 * @date 2021/5/17 11:29
 */
@DubboService
public class UserMessageServiceImpl extends ServiceImpl<UserMessageMapper, UserMessage> implements IUserMessageService {

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Override
    public Result<PageResponse<UserMessageListDTO>> queryPage(UserMessagePageReq req) {
        Page<UserMessage> queryPage = new Page(req.getPageNo(), req.getPageSize());
        Page<UserMessage> page = userMessageMapper.selectPage(queryPage, new QueryWrapper<UserMessage>()
                .lambda().eq(UserMessage::getUserId, req.getUserId())
        .eq(req.getType() != null,UserMessage::getType, req.getType())
        .eq(UserMessage::getIsDeleted, IsDeletedCode.NO.getCode()));

        List<UserMessageListDTO> listDTOS = EntityUtil.copyList(page.getRecords(), UserMessageListDTO.class);
        PageResponse<UserMessageListDTO> result = new PageResponse<>(listDTOS,
                page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() *
                page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
        return Result.success(result);
    }

    @Override
    public CompletableFuture<Result> notify(UserMessageReq req) {
        return CompletableFuture.supplyAsync(() -> {
            UserMessage userMessage = EntityUtil.copy(req, UserMessage.class);
            LocalDateTime now = LocalDateTime.now();
            userMessage.setId(SnowflakeIdWorker.nextId());
            userMessage.setIsRead(IsRead.UNREAD.getCode());
            userMessage.setCreateId(userMessage.getUserId());
            userMessage.setModifiedId(userMessage.getUserId());
            userMessage.setIsDeleted(IsDeletedCode.NO.getCode());
            userMessage.setId(SnowflakeIdWorker.nextId());
            userMessage.setGmtCreate(now);
            userMessage.setGmtModified(now);
            userMessageMapper.insert(userMessage);
            return Result.success();
        });
    }

    @Override
    public CompletableFuture<Result> orderNotify(Long userId, String title, String content,Long orderId,Integer businessType) {
        UserMessageReq req = new UserMessageReq();
        req.setUserId(userId);
        req.setType(InformType.ORDER.getCode());
        req.setTitle(title);
        req.setContent(content);
        req.setOrderId(orderId);
        req.setBusinessType(businessType);
        return notify(req);
    }

    @Override
    public CompletableFuture<Result> sysNotify(Long userId, String title, String content) {
        UserMessageReq req = new UserMessageReq();
        req.setUserId(userId);
        req.setType(InformType.SYSTEM.getCode());
        req.setTitle(title);
        req.setContent(content);
        return notify(req);
    }

    @Override
    public Result<UserMessageDTO> detail(Long id, Long userId) {
        UserMessage userMessage = userMessageMapper.selectOne(new QueryWrapper<UserMessage>().lambda()
                .eq(UserMessage::getId, id)
                .eq(UserMessage::getUserId, userId)
                .eq(UserMessage::getIsDeleted, IsDeletedCode.NO.getCode()));
        if(userMessage != null) {
            userMessage.setIsRead(IsRead.READ.getCode());
            userMessageMapper.update(userMessage,Wrappers.<UserMessage>lambdaUpdate()
                    .eq(UserMessage::getId, id));
        }
        UserMessageDTO userMessageDTO = EntityUtil.copy(userMessage, UserMessageDTO.class);
        return Result.success(userMessageDTO);
    }

    @Override
    public Integer countByUserId(Long userId) {
        Integer count = baseMapper.selectCount(Wrappers.<UserMessage>lambdaQuery()
                .eq(UserMessage::getUserId, userId)
                .eq(UserMessage::getIsDeleted, IsDeletedCode.NO.getCode())
                .eq(UserMessage::getIsRead, IsRead.UNREAD.getCode()));
        return count == null?0:count;
    }
}
